﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management;

#region   变量类模块
/// <summary>
/// **********************************************************************}
///                   HardCls   自定义网络类模块描述                        }
///   				 Ver:1.0          Author: HeavenBen                   }
///                   Last Version:Ver:1.0.0B                             }
///      Create Date:2011-09-21        Ex. Date: 2011-09-21               }
///-----------------------------------------------------------------------}
///    Description:                                                       }
///     请以此命名约定来规范整个系统框架                                    }
///     1.所有的模块工程前缀为Thinking.来命名                               }
///     2.所有功能性的涵盖空间命名以.之后的命名来规范。                      }
///     3.方法、属性等名称尽量以简约、易懂的方法来命名。                     }
///     Update Detail List:                                               }
/// Update Version    |   Last ModifyDate   |   Modify Person  |   Remark }
///                                                                       }
///-----------------------------------------------------------------------}
///		    CopyRight (C) 2011  Thinking Co.Ltd.                      	  }
///***********************************************************************}
/// </summary>
#endregion
namespace Thinking.Core
{
    class HardCls
    {
        #region 获取CPU信息
        //CPU信息
        public static string GetCpuInfo()
        {
            string sCpuInfo = "";
            try
            {

                ManagementObjectSearcher _CPUSearcher = new ManagementObjectSearcher("SELECT * FROM Win32_Processor");
                foreach (ManagementObject _CpuItem in _CPUSearcher.Get())
                {
                    sCpuInfo += _CpuItem["Name"].ToString().Trim();
                    _CpuItem.Dispose();
                    break;
                }

                _CPUSearcher.Dispose();
            }
            catch (System.Exception ex)
            {
                return sCpuInfo;
            }
            return sCpuInfo;
        }
        #endregion

        #region 获取主板信息
        //主板信息
        public static string GetMainBoardInfo()
        {
            string sMainBoardInfo = "";
            try
            {

                ManagementObjectSearcher _MainBoardSearcher = new ManagementObjectSearcher("SELECT * FROM Win32_BaseBoard");
                ManagementObjectCollection.ManagementObjectEnumerator _MainBoard = _MainBoardSearcher.Get().GetEnumerator();
                _MainBoard.MoveNext();
                ManagementBaseObject _MainBoardObj = _MainBoard.Current;
                sMainBoardInfo = (String)_MainBoardObj.GetPropertyValue("Manufacturer").ToString().Trim() + "-" + (String)_MainBoardObj.GetPropertyValue("Product").ToString().Trim();
                _MainBoardObj.Dispose();
                _MainBoard.Dispose();
                _MainBoardSearcher.Dispose();
            }
            catch (System.Exception ex)
            {
                return sMainBoardInfo;
            }
            return sMainBoardInfo;
        }
        #endregion

        #region 获取声卡信息
        //声卡信息
        public static string GetSoundCardInfo()
        {
            string sResult = "";
            try
            {

                ManagementObjectSearcher MySearcher = new ManagementObjectSearcher("SELECT * FROM Win32_SoundDevice");
                foreach (ManagementObject MyObject in MySearcher.Get())
                {
                    sResult += MyObject["ProductName"].ToString().Trim();
                    MyObject.Dispose();
                    break;
                }
                MySearcher.Dispose();
            }
            catch (System.Exception ex)
            {
                return sResult;
            }
            return sResult;
        }
        #endregion

        #region 获取显卡信息
        //显卡
        public static string GetVideoCardInfo()
        {
            string sResult = "";
            try
            {

                ManagementObjectSearcher MySearcher = new ManagementObjectSearcher("SELECT * FROM Win32_VideoController");
                foreach (ManagementObject MyObject in MySearcher.Get())
                {
                    sResult += MyObject["Name"].ToString().Trim();
                    MyObject.Dispose();
                    break;
                }
                MySearcher.Dispose();
            }
            catch (System.Exception ex)
            {
                return sResult;
            }
            return sResult;
        }
        #endregion

        #region 获取内存信息
        //内存
        public static string GetMemoryInfo()
        {
            string sResult = "";
            int iValue = 0;
            try
            {

                ManagementObjectSearcher MySearcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMemory");
                foreach (ManagementObject MyObject in MySearcher.Get())
                {
                    if (MyObject["Capacity"] != null)
                    {
                        iValue += ValCls.StrToInt(MyObject["Capacity"].ToString());
                    }
                    MyObject.Dispose();
                }
                sResult += (iValue / 1024 / 1024) + "M";
                MySearcher.Dispose();
            }
            catch (System.Exception ex)
            {
                return sResult;
            }
            return sResult;
        }
        #endregion

        #region 获取当前屏幕的分辨率
        //当前分辨率
        public static string GetScreenResolutionInfo()
        {
            string sResult = "";
            try
            {

                ManagementObjectSearcher MySearcher = new ManagementObjectSearcher("SELECT * FROM Win32_VideoController");
                foreach (ManagementObject MyObject in MySearcher.Get())
                {
                    sResult += MyObject["VideoModeDescription"].ToString();
                    MyObject.Dispose();
                    break;
                }
                MySearcher.Dispose();
            }
            catch (System.Exception ex)
            {
                return sResult;
            }
            return sResult;
        }
        #endregion

        #region 获取网卡IP
        //网卡IP
        public static string GetNetCardIP()
        {
            string sResult = "";
            string sCheckSame = "";
            bool bFound = false;
            try
            {

                ManagementObjectSearcher MySearcher = new ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled='TRUE'");

                foreach (ManagementObject MyObject in MySearcher.Get())
                {
                    string[] addresses = (string[])MyObject["IPAddress"];

                    foreach (string ipaddress in addresses)
                    {
                        if (ipaddress != "0.0.0.0" && sCheckSame != ipaddress)
                        {
                            sCheckSame = ipaddress;
                            sResult += ipaddress + "/";
                            bFound = true;
                            break;
                        }
                    }


                    MyObject.Dispose();
                    if (bFound)
                    {
                        break;
                    }
                }
                if (sResult != null && sResult != "")
                {
                    sResult = sResult.Remove(sResult.LastIndexOf("/"));
                }

                MySearcher.Dispose();
            }
            catch (System.Exception ex)
            {
                return sResult;
            }
            return sResult;
        }
        #endregion

        #region 获取网卡MAC信息
        //网卡MAC
        public static string GetNetCardMac()
        {
            string sResult = "";
            string sCheckSame = "";
            bool bFound = false;
            try
            {

                ManagementObjectSearcher MySearcher = new ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled='TRUE'");
                foreach (ManagementObject MyObject in MySearcher.Get())
                {
                    if (MyObject["MACAddress"] != null && MyObject["MACAddress"].ToString() != null)
                    {
                        if (sCheckSame != MyObject["MACAddress"].ToString())
                        {
                            sCheckSame = MyObject["MACAddress"].ToString();
                            sResult += MyObject["MACAddress"].ToString().Trim() + "/";
                            bFound = true;
                        }
                    }

                    MyObject.Dispose();
                    if (bFound)
                    {
                        break;
                    }
                }
                if (sResult != null && sResult != "")
                {
                    sResult = sResult.Remove(sResult.LastIndexOf("/"));
                }
                MySearcher.Dispose();
            }
            catch (System.Exception ex)
            {
                return sResult;
            }
            return sResult;
        }
        #endregion
    }
}
